package com.aeye.mbr.upms.server.controller.manage;

import com.aeye.mbr.common.base.BaseController;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.ui.validator.LengthValidator;
import com.aeye.mbr.upms.common.constant.UpmsResult;
import com.aeye.mbr.upms.common.constant.UpmsResultConstant;
import com.aeye.mbr.upms.dao.model.UpmsSystem;
import com.aeye.mbr.upms.dao.model.UpmsTerminal;
import com.aeye.mbr.upms.rpc.api.UpmsAreaService;
import com.aeye.mbr.upms.rpc.api.UpmsTerminalService;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 区划controller
 * Created by shuaeye.mbr on 2017/2/6.
 */
@Controller
@RequestMapping("/manage/terminal")
public class UpmsTerminalController extends BaseController {

    private static Logger _log = LoggerFactory.getLogger(UpmsTerminalController.class);

    @Autowired
    private UpmsTerminalService upmsTerminalService;

    @RequiresPermissions("upms:terminal:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/terminal/index.jsp";
    }

    @RequiresPermissions("upms:terminal:read")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(
            BootstrapTableParams params) {
        return upmsTerminalService.selectUpmsTerminalListByParams(params);
    }


    @RequiresPermissions("upms:terminal:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "/manage/terminal/create.jsp";
    }

    @RequiresPermissions("upms:terminal:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(UpmsTerminal upmsTerminal) {
        ComplexResult result = FluentValidator.checkAll()
                .on(upmsTerminal.getName(), new LengthValidator(1, 20, "终端名称"))
                .on(upmsTerminal.getVersion(), new LengthValidator(1, 20, "版本"))
                .on(upmsTerminal.getUrl(), new LengthValidator(1, 20, "下载地址"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        int count = upmsTerminalService.insertSelective(upmsTerminal);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @RequiresPermissions("upms:system:delete")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = upmsTerminalService.deleteByPrimaryKeys(ids);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @RequiresPermissions("upms:terminal:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        UpmsTerminal terminal = upmsTerminalService.selectByPrimaryKey(id);
        modelMap.put("terminal", terminal);
        return "/manage/terminal/update.jsp";
    }

    @RequiresPermissions("upms:terminal:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") int id, UpmsTerminal upmsTerminal) {
        ComplexResult result = FluentValidator.checkAll()
                .on(upmsTerminal.getName(), new LengthValidator(1, 20, "终端名称"))
                .on(upmsTerminal.getVersion(), new LengthValidator(1, 20, "版本"))
                .on(upmsTerminal.getUrl(), new LengthValidator(1, 20, "下载地址"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        upmsTerminal.setId(id);
        int count = upmsTerminalService.updateByPrimaryKeySelective(upmsTerminal);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }
}
